package com.example.lenovo.qm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class GridViewAdapter extends BaseAdapter {
    private ArrayList<GridViewItemData> mArrayGridViewItemData;

    private Context context;
    private LayoutInflater layoutInflater;

    public ArrayList<GridViewItemData> getmArrayGridViewItemData() {
        return mArrayGridViewItemData;
    }

    public void setmArrayGridViewItemData(ArrayList<GridViewItemData> mArrayGridViewItemData) {
        this.mArrayGridViewItemData = mArrayGridViewItemData;
    }

    public GridViewAdapter(Context context, ArrayList<GridViewItemData> mArrayGridViewItemData) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mArrayGridViewItemData=mArrayGridViewItemData;
    }

    @Override
    public int getCount() {

        return mArrayGridViewItemData.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayGridViewItemData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.grid_item, null);
            convertView.setTag(new GridViewAdapter.ViewHolder(convertView));
        }
        initializeViews((GridViewItemData) getItem(position), (GridViewAdapter.ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(GridViewItemData object, GridViewAdapter.ViewHolder holder) {
        holder.imageView.setImageResource(object.imgId);
        holder.textView.setText(object.text);

    }

    protected class ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.imageView);
            textView = (TextView) view.findViewById(R.id.textView);

        }
    }
}
